//
//  PrivateMessages.m
//  百车易车险
//
//  Created bychenxu on 14-8-29.
//  Copyright (c) 2014年 chenxu. All rights reserved.
//

#import "PrivateMessages.h"

@implementation PrivateMessages
- (instancetype)init
{
    self = [super init];
    if (self) {
        _propertyArrayMap = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"PrivateMessage", @"list", nil];
        _canLoadMore = YES;
        _isLoading = _willLoadMore = NO;
        _page = [NSNumber numberWithInteger:1];
        _pageSize = [NSNumber numberWithInteger:30];
        _orderid = nil;
    }
    return self;
}

/*!
 *  @brief  为了持久化用的
 *  @param order <#order description#>
 *  @return <#return value description#>
 */
+ (PrivateMessages *)priMsgsWithOrder:(NSString *)order{
    PrivateMessages *priMsgs = [[PrivateMessages alloc] init];
    priMsgs.orderid = order;
    return priMsgs;
}
/*!
 *  @brief  根据订单号存消息
 *  @return 存本地时候的key名
 */
- (NSString *)localPrivateMessagesPath{
    NSString *path;
    if (_orderid) {
        path = [NSString stringWithFormat:@"conversations_%@", _orderid];
    }else{
        path = @"conversations";
    }
    return path;
}


- (NSDictionary *)toParams{
    return @{@"page" : _willLoadMore? [NSNumber numberWithInt:_page.intValue +1]: [NSNumber numberWithInt:1],
             @"pageSize" : _pageSize};
}
- (void)configWithObj:(PrivateMessages *)priMsgs{
    self.page = priMsgs.page;
    self.pageSize = priMsgs.pageSize;
    self.totalPage = priMsgs.totalPage;
    if (_willLoadMore) {
        [self.list addObjectsFromArray:priMsgs.list];
    }else{
        self.list = [NSMutableArray arrayWithArray:priMsgs.list];
    }
    _canLoadMore = _page.intValue < _totalPage.intValue;
}

- (void)sendNewMessage:(PrivateMessage *)nextMsg{
    if (!_nextMessages) {
        _nextMessages = [[NSMutableArray alloc] initWithCapacity:1];
    }
    if (!_list) {
        _list = [[NSMutableArray alloc] initWithCapacity:1];
    }
    NSUInteger index = [_nextMessages indexOfObject:nextMsg];
    
    if (index == NSNotFound) {
        [_nextMessages insertObject:nextMsg atIndex:0];
        [_list insertObject:nextMsg atIndex:0];
    }
    else
    {
        if (index != 0) {
            [_nextMessages exchangeObjectAtIndex:index withObjectAtIndex:0];
        }
        NSUInteger indexInList = [_list indexOfObject:nextMsg];
        if (indexInList && indexInList!= 0) {
            [_list exchangeObjectAtIndex:indexInList withObjectAtIndex:0];
        }
    }
    
}

/*!
 *  @brief  从next队列存到list队列
 *  @param sucessMsg <#sucessMsg description#>
 *  @param oldMsg    <#oldMsg description#>
 */
- (void)sendSuccessMessage:(PrivateMessage *)sucessMsg andOldMessage:(PrivateMessage *)oldMsg{
    if (_nextMessages) {
        [_nextMessages removeObject:oldMsg];
    }
    NSUInteger index = [_list indexOfObject:oldMsg];
    if (index != NSNotFound) {
        [_list replaceObjectAtIndex:index withObject:sucessMsg];
    }
}
- (void)deleteMessage:(PrivateMessage *)msg{
    [_list removeObject:msg];
}

- (void)receiveNewMessage:(PrivateMessage *)Msg{
    if (!_list) {
        _list = [[NSMutableArray alloc] initWithCapacity:1];
    }
    [_list insertObject:Msg atIndex:0];
    Msg.sendStatus = PrivateMessageStatusSendSucess;

}


#pragma mark  NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder {

    [aCoder encodeObject:_list forKey:@"msgs_list"];
    [aCoder encodeObject:_nextMessages forKey:@"msg_nextmsgs"];
    [aCoder encodeObject:_orderid forKey:@"msg_orderid"];
    // [aCoder encodeObject:_sendStatus forKey:@"msg_sendstatus"];
    
    
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    _list = [aDecoder decodeObjectForKey:@"msgs_list"];
    _nextMessages = [aDecoder decodeObjectForKey:@"msg_nextmsgs"];
    //  _sender = [aDecoder decodeObjectForKey:@"msg_sender"];
    _orderid = [aDecoder decodeObjectForKey:@"msg_orderid"];
    return self;
}
@end














